package org.ank.food;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ProfileChooseActivity extends Activity {
	
	public static final int MENU_ITEM_EDIT = Menu.FIRST;
    public static final int MENU_ITEM_DELETE = Menu.FIRST + 1;
    public static final int MENU_ITEM_CREATE = Menu.FIRST;
    public static final int MENU_ITEM_HELP = Menu.FIRST + 1;
    public static final int MENU_ITEM_ABOUT = Menu.FIRST + 2;
    public static final int MENU_ITEM_EXIT = Menu.FIRST + 3;
    
    public static final int CALLER_VIEW_PROFILE = 0;
    public static final String INTENT_EXTRA_NAME = "caller";
    
	private ListView profileList;
	private TextView title, listItem;
	private Button createButton, infoButton, exitButton;
	private SimpleCursorAdapter adapter;
	private ProfileManager profileManager;
	private Cursor profilesCursor = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent parentIntent = getIntent(); 
        
        setContentView(R.layout.profile_choose_activity);
        
        
        
        if(parentIntent.getIntExtra(INTENT_EXTRA_NAME, -1) == CALLER_VIEW_PROFILE)
        	Toast.makeText(getApplicationContext(), R.string.toast_profile_saved,
					Toast.LENGTH_SHORT).show();
        profileManager = ProfileManager.getInstance(getApplicationContext());
        profileList = (ListView)findViewById(R.id.profile_list);
        title = (TextView)findViewById(R.id.profile_choose_title);

        createButton = (Button)findViewById(R.id.profile_choose_no_profiles_button);
//        exitButton = (Button)findViewById(R.id.profile_choose_exit_button);
//        infoButton = (Button)findViewById(R.id.profile_choose_info_button);
        

        
//        listItem = (TextView)findViewById(R.id.profile_list_item_text);
//        ListView
        
//        profileManager = ProfileManager.getInstance(getApplicationContext());
//        AdviceManager adviceManager = AdviceManager.getInstance(getApplicationContext());

        profilesCursor = profileManager.getProfilesCursor();
        if(profilesCursor.getCount() == 0){
        	createButton.setVisibility(Button.VISIBLE);
        	createButton.setOnClickListener(new Button.OnClickListener(){
            	public void onClick(View v){
            		Intent intent = new Intent();
//            		intent.setClass(getApplicationContext(), EnterNameActivity.class);
            		intent.setClass(getApplicationContext(), ViewProfileActivity.class);
            		startActivity(intent);
            		profilesCursor.close();
            		finish();
            	}
            });
        }else{
        	createButton.setVisibility(Button.INVISIBLE);
        	adapter = new SimpleCursorAdapter(getApplicationContext(),
        		android.R.layout.simple_list_item_1, 
    			profilesCursor,
    			new String[]{ProfileProvider.COLUMN_NAMES[ProfileProvider.NAME]},
    			new int[] {android.R.id.text1});

        		adapter = profileManager.getProfilesAdapter();

        	profileList.setAdapter(adapter);
        	profileList.setOnItemClickListener(new OnItemClickListener(){
        		@Override
        		public void onItemClick(AdapterView<?> adapterView, View listItem, int position, long id) {
//        			Log.v("PCA",listItem.getClass().toString());
//        			TextView textListItem = (TextView)listItem;  //((ViewGroup)listItem).getChildAt(1);
        			profileManager.setCurrentProfile(/*textListItem.getText().toString()*/(int)id);
//        			Log.v("pca",textListItem.getText().toString());
        			Intent intent = new Intent();
        			intent.setClass(getApplicationContext(), EatenTodayActivity.class);
//        			intent.putExtra(LastAdviceActivity.ADVICE_EXTRA_NAME,
//            				LastAdviceActivity.EXISTING_ADVICE);
        			startActivity(intent);
        			profilesCursor.close();
        			finish();
        		}
        	});
        	profileList.setOnCreateContextMenuListener(this);
        	
//        	createButton.setOnClickListener(new Button.OnClickListener(){
//        		public void onClick(View v){
//        			Intent intent = new Intent();
//        			intent.setClass(getApplicationContext(), EnterNameActivity.class);
//        			startActivity(intent);
////        			finish();
//        		}
//        	});
//        	exitButton.setOnClickListener(new Button.OnClickListener(){
//        		public void onClick(View v){
//        			finish();
//        		}
//        	});
//        	infoButton.setOnClickListener(new Button.OnClickListener(){
//        		public void onClick(View v){
//        			Intent intent = new Intent();
//        			intent.setClass(getApplicationContext(), InfoActivity.class);
//        			startActivity(intent);
////        			finish();
//        		}
//        	});
        }
    }
    
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		AdapterView.AdapterContextMenuInfo info;
        try {
             info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        } catch (ClassCastException e) {
//            Log.e(TAG, "bad menuInfo", e);
            return;
        }

        Cursor cursor = (Cursor)adapter.getItem(info.position);
        if (cursor == null) {
            // For some reason the requested item isn't available, do nothing
            return;
        }
        menu.setHeaderTitle(cursor.getString(0));
		menu.add(0, MENU_ITEM_EDIT, 0, R.string.context_menu_edit);
		menu.add(0, MENU_ITEM_DELETE, 0, R.string.profile_choose_context_menu_delete);
	}
    public boolean onContextItemSelected(MenuItem item){
    	final AdapterView.AdapterContextMenuInfo info;
//    	ProfileManager profileManager = ProfileManager.getInstance(getApplicationContext());
        try {
             info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        } catch (ClassCastException e) {
//            Log.e(TAG, "bad menuInfo", e);
            return false;
        }

        switch (item.getItemId()) {
            case MENU_ITEM_DELETE: 

            	AlertDialog.Builder builder = new AlertDialog.Builder(ProfileChooseActivity.this);
				builder.setMessage(getString(R.string.profile_delete_alert_message));
				builder.setNegativeButton(R.string.profile_create_alert_dialog_negative_button_text, 
						new DialogInterface.OnClickListener(){
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				builder.setPositiveButton(R.string.profile_create_alert_dialog_positive_button_text, 
						new DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog,	int which) {
							profileManager.deleteProfile((int)info.id);
//			                adapter = profileManager.getProfilesCursor();
			                profilesCursor = profileManager.getProfilesCursor(); 
			                if(profilesCursor.getCount() == 0){
			                	profileManager.deleteProfile((int)info.id);
			                    adapter = profileManager.getProfilesAdapter();
			                	createButton.setVisibility(Button.VISIBLE);
			                	createButton.setOnClickListener(new Button.OnClickListener(){
			                    	public void onClick(View v){
			                    		Intent intent = new Intent();
			                    		intent.setClass(getApplicationContext(), ViewProfileActivity.class);
			                    		intent.putExtra(ViewProfileActivity.INTENT_EXTRA_NAME, 
			                    				ViewProfileActivity.CALLER_PROFILE_CHOOSE);
			                    		startActivity(intent);
			                    		finish();
			                    	}
			                	});
			                }
			                adapter = new SimpleCursorAdapter(getApplicationContext(),
			                		android.R.layout.simple_list_item_1, 
			            			profilesCursor,
			            			new String[]{ProfileProvider.COLUMN_NAMES[ProfileProvider.NAME]},
			            			new int[] {android.R.id.text1});
			                
						}
					});
//				Log.v("pca","onfo id = "+info.id);
				builder.setTitle(profileManager.getProfileNameById((int)info.id));
				
				builder.show();
//            	profileManager.deleteProfile((int)info.id);
////                adapter = profileManager.getProfilesCursor();
//                profilesCursor = profileManager.getProfilesCursor(); 
//                if(profilesCursor.getCount() == 0){
//                	createButton.setVisibility(Button.VISIBLE);
//                	createButton.setOnClickListener(new Button.OnClickListener(){
//                    	public void onClick(View v){
//                    		Intent intent = new Intent();
//                    		intent.setClass(getApplicationContext(), EnterNameActivity.class);
//                    		startActivity(intent);
//                    		finish();
//                    	}
//                	});
//                }
//                adapter = new SimpleCursorAdapter(getApplicationContext(),
//                		android.R.layout.simple_list_item_1, 
//            			profilesCursor,
//            			new String[]{ProfileProvider.COLUMN_NAMES[ProfileProvider.NAME]},
//            			new int[] {android.R.id.text1});
                
                return true;
            case MENU_ITEM_EDIT:
//            	LinearLayout l = (LinearLayout)info.targetView;
//            	TextView tv = (TextView) (l.getChildAt(0));
            	profileManager.setCurrentProfile(/*tv.getText().toString()*/(int)info.id);
            	profileManager.setProfileMode(ProfileManager.PROFILE_MODE_EDIT);
            	Intent intent = new Intent();
        		intent.setClass(getApplicationContext(), ViewProfileActivity.class);
//        		intent.putExtra(EnterNameActivity.CALL_FROM_FOLLOWING, false);
        		startActivity(intent);
        		profilesCursor.close();
        		finish();
        		return true;
        }
        return false;
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_ITEM_CREATE, 0, R.string.options_menu_create).setIcon(android.R.drawable.ic_menu_add);
        menu.add(1, MENU_ITEM_HELP, 0, R.string.options_menu_help).setIcon(android.R.drawable.ic_menu_help);
        menu.add(1, MENU_ITEM_ABOUT, 0, R.string.options_menu_about).setIcon(android.R.drawable.ic_menu_info_details);
        menu.add(1, MENU_ITEM_EXIT, 0, R.string.options_menu_exit).setIcon(android.R.drawable.ic_menu_revert);
        
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent intent;
        switch (item.getItemId()) {
        case MENU_ITEM_CREATE:
        	profileManager.setProfileMode(ProfileManager.PROFILE_MODE_CREATE);
        	intent = new Intent();
    		intent.setClass(getApplicationContext(), ViewProfileActivity.class);
    		intent.putExtra(ViewProfileActivity.INTENT_EXTRA_NAME, 
    				ViewProfileActivity.CALLER_PROFILE_CHOOSE);
    		startActivity(intent);
    		profilesCursor.close();
    		finish();
            return true;
        case MENU_ITEM_HELP:
        	intent = new Intent();
			intent.setClass(getApplicationContext(), InfoActivity.class);
			startActivity(intent);
			profilesCursor.close();
			finish();
			return true;
        case MENU_ITEM_ABOUT:
        	
        	return true;
        case MENU_ITEM_EXIT:
        	profilesCursor.close();
        	finish();
        	return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
   
}